package targetToOffer.tenthDay;

import sun.security.provider.DSAKeyFactory;

/**
 * @author Godc
 * @description
 */
public class KthLargest {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(4);
        TreeNode node3 = new TreeNode(2);
//        TreeNode node4 = new TreeNode(4);
//        TreeNode node5 = new TreeNode(1);
        root.left = node1;
        root.right = node2;
//        node1.left = node3;
        node1.right = node3;
//        node3.left = node5;
        KthLargestSolution s = new KthLargestSolution();
        int i = s.kthLargest(root, 1);
        System.out.println(i);
    }
}
class KthLargestSolution {
    int res;
    int kth;
    public int kthLargest(TreeNode root, int k) {
        kth = k;
        dfs(root);
        return res;
    }
    public void dfs(TreeNode root){
        // 找到了，返回
        if(root==null||kth==0) return;
        dfs(root.right);
        if(--kth==0) res = root.val;
        dfs(root.left);
    }
}